﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TeaTimer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public TimeSpan timespan = new TimeSpan(0, 0, 0);
        public DateTime starttime, lasttrigger;
        Timer timer = new Timer();
        System.Threading.Thread soundloopthread;
        public bool isrunning = false;

        #region supporting code
        [System.Runtime.InteropServices.DllImport("winmm.DLL", EntryPoint = "PlaySound", SetLastError = true)]
        private static extern bool PlaySound(string szSound, System.IntPtr hMod, PlaySoundFlags flags);

        [System.Flags]
        public enum PlaySoundFlags : int
        {
            SND_SYNC = 0x0000,
            SND_ASYNC = 0x0001,
            SND_NODEFAULT = 0x0002,
            SND_LOOP = 0x0008,
            SND_NOSTOP = 0x0010,
            SND_NOWAIT = 0x00002000,
            SND_FILENAME = 0x00020000,
            SND_RESOURCE = 0x00040004
        }
        #endregion
        private void startbutton_Click(object sender, EventArgs e)
        {
            if (!isrunning)
            {
                starttime = DateTime.Now; lasttrigger = DateTime.Now;
                timer.Interval = 1000;
                timer.Tick += new EventHandler(timer_Tick);
                timer.Start();
                isrunning = true;
                startbutton.Text = "Stop";
            }
            else
            {
                startbutton.Text = "Start";
                timer.Stop();
                isrunning = false;
            }

        }
        
        public  void timer_Tick(object sender, EventArgs e)
        {
            
            timespan = DateTime.Now - starttime;
            label4.Text = "Next alarm in : " + Math.Round(Convert.ToInt32(numericUpDown1.Value) - (DateTime.Now - lasttrigger).TotalMinutes, 1) + " Minutes.";
            label1.Text = (timespan.Hours.ToString() + ":" + timespan.Minutes.ToString() + ":" + timespan.Seconds.ToString());
            if ((DateTime.Now - lasttrigger).TotalMinutes > Convert.ToInt32(numericUpDown1.Value))
            {
                lasttrigger = DateTime.Now;

                if ((soundloopthread == null))
                {
                    if (checkBox_alarmsound.Checked && System.IO.File.Exists(textBox_soundonalarm.Text))
                    {
                        if (checkBox_loopsound.Checked)
                        {
                            soundpath = textBox_soundonalarm.Text;
                            soundloopthread = new System.Threading.Thread(new System.Threading.ThreadStart(playsoundlooped));
                            soundloopthread.Start();
                        }
                        else
                            PlaySound(textBox_soundonalarm.Text, new IntPtr(), PlaySoundFlags.SND_ASYNC);
                    }
                    MessageBox.Show("Trrrrrrrrrriiiiiiing~", "Timer nudge tiemz");
                    soundloopthread.Abort();
                    soundloopthread = null;
                }

                

                
            }
        }
        static string soundpath;
        static void playsoundlooped()
        {
            while(true)
            PlaySound(soundpath, new IntPtr(), PlaySoundFlags.SND_SYNC);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            lasttrigger = DateTime.Now;
            
        }

        private void checkBox_alarmsound_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_alarmsound.Checked)
                groupBox_alarmsoundsettings.Enabled = true;
            else
                groupBox_alarmsoundsettings.Enabled = false;
        }

        private void button_browseforsound_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Wave sound|*.wav";
            

            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    PlaySound(ofd.FileName, new IntPtr(), PlaySoundFlags.SND_ASYNC);
                    textBox_soundonalarm.Text = ofd.FileName;

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error");
                }
            }
        }


        


    }
}
